package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.storeRoom.domain.Shelf;
import cn.yuanqiao.archive.storeRoom.domain.StoreRole;
import cn.yuanqiao.archive.storeRoom.domain.Storeroom;
import cn.yuanqiao.archive.storeRoom.mapper.ShelfMapper;
import cn.yuanqiao.archive.storeRoom.mapper.StoreRoleMapper;
import cn.yuanqiao.archive.storeRoom.mapper.StoreroomMapper;
import cn.yuanqiao.archive.storeRoom.service.IStoreroomService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库房信息Service业务层处理
 * 
 * @author xy
 * @date 2023-06-16
 */
@Service
public class StoreroomServiceImpl implements IStoreroomService
{
    private static final Logger log = LoggerFactory.getLogger(StoreroomServiceImpl.class);


    @Autowired
    private StoreroomMapper storeroomMapper;

    @Autowired
    private StoreRoleMapper storeRoleMapper;

    @Autowired
    private ShelfMapper shelfMapper;

    /**
     * 查询库房信息
     * 
     * @param storeId 库房信息主键
     * @return 库房信息
     */
    @Override
    public Storeroom selectStoreroomByStoreId(Long storeId)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        Storeroom storeroom=new Storeroom();
        if(StringUtils.isEmpty(tenantCode)){
            throw new ServiceException("TenantCode is null");
           /* List<Long> list=storeRoleMapper.getStoreUserList(storeId,"");
            storeroom=storeroomMapper.selectStoreroomByStoreId(storeId,"");
            storeroom.setUserId(list);
            return storeroom;*/
        }else {
            List<Long> list=storeRoleMapper.getStoreUserList(storeId,tenantCode.toUpperCase());
            storeroom=storeroomMapper.selectStoreroomByStoreId(storeId,tenantCode.toUpperCase());
            storeroom.setUserId(list);
             return  storeroom;
        }
    }

    /**
     * 查询库房信息列表
     * 
     * @param storeroom 库房信息
     * @return 库房信息
     */
    @Override
    public List<Storeroom> selectStoreroomList(Storeroom storeroom)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            throw new ServiceException("TenantCode is null");
            //return storeroomMapper.selectStoreroomList(storeroom,"");
        }else {
            return storeroomMapper.selectStoreroomListByRole(storeroom,tenantCode.toUpperCase(),SecurityUtils.getUserId());

        }
    }

    /**
     * 新增库房信息
     * 
     * @param storeroom 库房信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map insertStoreroom(Storeroom storeroom)
    {
        Map map=new HashMap();
        String tenantCode = SecurityUtils.getTenantCode();

        //此处加验证，通不过验证则抛出异常
        String storeCode=storeroom.getStoreCode();
        int count=storeroomMapper.getCountByCode(storeCode,tenantCode.toUpperCase());
        if(count>0){
            log.error("库房编号已存在!");
            map.put("code","500");
            map.put("msg","库房编号已存在!");
           return map;
          //  throw new ServiceException("库房编码已存在!");
        }

        storeroom.setIsDel(0);
        storeroom.setStoreId(SnowflakeIdGenerator.generateId());
        storeroom.setCreator(SecurityUtils.getUserId());
        storeroom.setCreateTime(DateUtils.getNowDate());
        //将权限新增新增进去
        List<Long> userIdList=storeroom.getUserId();

        for(Long userId:userIdList){
            StoreRole storeRole =new StoreRole();
            storeRole.setStoreId(storeroom.getStoreId());
            storeRole.setUserId(userId);
            storeRoleMapper.insertStoreRole(storeRole);
        }
        if(StringUtils.isEmpty(tenantCode)) {
            throw new ServiceException("TenantCode is null");
           // return storeroomMapper.insertStoreroom(storeroom,"");
        }else {
            if(storeroomMapper.insertStoreroom(storeroom,tenantCode.toUpperCase())>0){
                map.put("code","200");
                map.put("msg","成功!");
                return map;
            }else{
                map.put("code","500");
                map.put("msg","操作失败!");
                return map;
            }
         //   return storeroomMapper.insertStoreroom(storeroom,tenantCode.toUpperCase());
        }

    }

    /**
     * 修改库房信息
     * 
     * @param storeroom 库房信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStoreroom(Storeroom storeroom)
    {
        //验证该库房下面是否有柜子
        /*String count=;*/


        String tenantCode = SecurityUtils.getTenantCode();
        storeroom.setLastUpdater(SecurityUtils.getUserId());
        storeroom.setLastupdateTime(DateUtils.getNowDate());
        //将权限删除再进行重新赋权
        StoreRole storeRole=new StoreRole();
        storeRole.setStoreId(storeroom.getStoreId());
        storeRoleMapper.deleteStoreRoleByStoreId(storeRole);
        //将权限新增新增进去
        List<Long> userIdList=storeroom.getUserId();
        for(Long userId:userIdList){
            StoreRole role =new StoreRole();
            role.setStoreId(storeroom.getStoreId());
            role.setUserId(userId);
            storeRoleMapper.insertStoreRole(role);
        }
        if(StringUtils.isEmpty(tenantCode)) {
            throw new ServiceException("TenantCode is null");
           // return storeroomMapper.updateStoreroom(storeroom, "");
        }else{
            return storeroomMapper.updateStoreroom(storeroom,tenantCode.toUpperCase());

        }
    }

    /**
     * 批量删除库房信息
     * 
     * @param storeIds 需要删除的库房信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteStoreroomByStoreIds(Long[] storeIds)
    {
        String tenantCode = SecurityUtils.getTenantCode();
       /* StoreRole storeRole=new StoreRole();
        storeRole.setStoreId(storeroom.getStoreId());
        storeRoleMapper.deleteStoreRoleByStoreIds(storeIds);*/
        if(StringUtils.isEmpty(tenantCode)) {
            throw new ServiceException("TenantCode is null");
           /* storeRoleMapper.deleteStoreRoleByStoreIds(storeIds,"");
            return storeroomMapper.deleteStoreroomByStoreIds(storeIds,"");*/
        }else{
            //判断是否有柜子有柜子不能删除
            for(Long storeId:storeIds) {
                Shelf shelf = new Shelf();
                shelf.setStoreId(storeId);
                List<Shelf> shelfList = shelfMapper.selectShelfList(shelf, tenantCode);
                if(shelfList.size()>0){
                    throw new ServiceException("含有柜子的库房不能删除！");

                }
            }


            storeRoleMapper.deleteStoreRoleByStoreIds(storeIds,tenantCode.toUpperCase());
            return storeroomMapper.deleteStoreroomByStoreIds(storeIds,tenantCode.toUpperCase());

        }
    }

    /**
     * 删除库房信息信息
     * 
     * @param storeId 库房信息主键
     * @return 结果
     */
    @Override
    public int deleteStoreroomByStoreId(Long storeId)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            throw new ServiceException("TenantCode is null");
           // return storeroomMapper.deleteStoreroomByStoreId(storeId,"");
        }else{
            return storeroomMapper.deleteStoreroomByStoreId(storeId,tenantCode);

        }
    }

    @Override
    public int checkStore(Long storeId) {
        return storeroomMapper.checkStore(SecurityUtils.getTenantCode().toUpperCase(),storeId);
    }
}
